package com.sgb.kjwl.view.ui.adapter.self.order;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.ShoppingCarEntity;
import com.sgb.kjwl.view.widget.RoundImageView;
import com.swgk.core.util.StringUtil;

import java.util.List;

/**
 * 购物车的adapter
 * 因为使用的是ExpandableListView，所以继承BaseExpandableListAdapter
 * @author Administrator
 * by 2021.04.22
 */
public class CheckOrderListAdapter extends BaseExpandableListAdapter {

    private final Context context;

    private List<ShoppingCarEntity> data;
    private String goods_image = "";

    public CheckOrderListAdapter(Context context) {
        this.context = context;
    }

    /**
     * 自定义设置数据方法；
     * 通过notifyDataSetChanged()刷新数据，可保持当前位置
     *
     * @param data 需要刷新的数据
     */
    public void setData(List<ShoppingCarEntity> data) {
        this.data = data;
        notifyDataSetChanged();
    }

    @Override
    public int getGroupCount() {
        if (data != null && data.size() > 0) {
            return data.size();
        } else {
            return 0;
        }
    }

    @Override
    public Object getGroup(int groupPosition) {
        return data.get(groupPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public View getGroupView(final int groupPosition, final boolean isExpanded, View convertView, ViewGroup parent) {
        GroupViewHolder groupViewHolder;
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.item_check_order_group, null);

            groupViewHolder = new GroupViewHolder(convertView);
            convertView.setTag(groupViewHolder);
        } else {
            groupViewHolder = (GroupViewHolder) convertView.getTag();
        }
        final ShoppingCarEntity datasBean = data.get(groupPosition);

        //店铺名称
        String store_name = datasBean.getShopName();

        if (store_name != null) {
            groupViewHolder.tvStoreName.setText(store_name);
        } else {
            groupViewHolder.tvStoreName.setText("");
        }
        return convertView;
    }

    static class GroupViewHolder {
        TextView tvStoreName;
        LinearLayout ll;

        GroupViewHolder(View view) {
            tvStoreName = view.findViewById(R.id.tv_store_name);
            ll = view.findViewById(R.id.ll);
        }
    }

    //------------------------------------------------子布局------------------------------------------------//
    @Override
    public int getChildrenCount(int groupPosition) {
        if (data.get(groupPosition).getData() != null && data.get(groupPosition).getData().size() > 0) {
            return data.get(groupPosition).getData().size();
        } else {
            return 0;
        }
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return data.get(groupPosition).getData().get(childPosition);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildViewHolder childViewHolder;
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.item_check_order_child, null);

            childViewHolder = new ChildViewHolder(convertView);
            convertView.setTag(childViewHolder);
        } else {
            childViewHolder = (ChildViewHolder) convertView.getTag();
        }
        final ShoppingCarEntity datasBean = data.get(groupPosition);

        //店铺是否在购物车中被选中
        final ShoppingCarEntity.DataBean goodsBean = datasBean.getData().get(childPosition);
        //商品图片
        if (goodsBean.getImageUrls()!=null  && goodsBean.getImageUrls().size()>0){
            goods_image = goodsBean.getImageUrls().get(0);
        }

        //商品价格promoteStatus为2取促销价
        String goods_price = "0";
        if (goodsBean.getPromoteStatus()==2){
            goods_price = StringUtil.showPrice(goodsBean.getPromotePrice());
        }else {
            goods_price = StringUtil.showPrice(goodsBean.getPrice());
        }
        //商品数量
        String goods_num = String.valueOf(goodsBean.getItemNum());
        String unit = goodsBean.getUnit();
        //商品名称
        String cartTitle = goodsBean.getCartTitle();
        Glide.with(context)
                .load(goods_image)
                .into(childViewHolder.ivImg);

        if (goodsBean.getPromoteStatus()==2) {
            childViewHolder.tvPrice.setText("￥"+goods_price+"元/"+unit);
            childViewHolder.tvPrice.setTextColor(context.getResources().getColor(R.color.color_f5455e));
            childViewHolder.skuTag.setVisibility(View.VISIBLE);
        } else {
            childViewHolder.tvPrice.setText("￥"+goods_price+"元/"+unit);
            childViewHolder.tvPrice.setTextColor(context.getResources().getColor(R.color.color_shopping_car_price));
            childViewHolder.skuTag.setVisibility(View.GONE);
        }
        if (goods_num != null) {
            childViewHolder.tvNum.setText("x"+goods_num);
        } else {
            childViewHolder.tvNum.setText("");
        }
        childViewHolder.strRemark.setText(goodsBean.getSkuStr());
        if (cartTitle != null){
            childViewHolder.tvTitle.setText(cartTitle);
        }else {
            childViewHolder.tvTitle.setText("");
        }
        if (childPosition == data.get(groupPosition).getData().size() - 1) {
            //childViewHolder.view.setVisibility(View.GONE);
            childViewHolder.viewLast.setVisibility(View.VISIBLE);
        } else {
            //childViewHolder.view.setVisibility(View.VISIBLE);
            childViewHolder.viewLast.setVisibility(View.GONE);
        }

        return convertView;
    }

    static class ChildViewHolder {
        RoundImageView ivImg;
        TextView tvTitle;
        TextView tvPrice;
        TextView tvNum;
        View viewLast;
        ImageView skuTag;
        TextView strRemark;

        ChildViewHolder(View views) {
            ivImg = views.findViewById(R.id.iv_img);
            tvTitle = views.findViewById(R.id.tv_product_name);
            tvPrice = views.findViewById(R.id.tv_price_value);
            skuTag = views.findViewById(R.id.sku_tag);
            tvNum = views.findViewById(R.id.tv_num);
            strRemark = views.findViewById(R.id.tv_name);
            viewLast = views.findViewById(R.id.view_last);
        }
    }
    //-----------------------------------------------------------------------------------------------//
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

}
